React input re-render 問題


Posted by Rich on 2021-09-02

在串 google 地圖 api 中增加搜尋列。但發現搜尋列每輸入一個值就會因為頁面重新渲染,造成 losing focus ,每打一個字就會歸零。
解決:方法來源 只要把 input 宣告在外面就可以了。
原因:因為把 input 宣告在 render 的 function 裡面。由於 re-render 就是重新呼叫一次函式,所以將 input 宣告在 function 裡面會變成重新宣告一個新的 input。所以每次 re-render 都會有一個新的 input。


#React







Related Posts

演習課 WEEK11

演習課 WEEK11

[ js 筆記 ] JS 中的 for ... in 和 for ... of

[ js 筆記 ] JS 中的 for ... in 和 for ... of

[cs50x - 2022] week5 資料結構

[cs50x - 2022] week5 資料結構


Comments